package day11.CRUD;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import org.junit.Test;

import java.sql.ResultSet;

public class Demo {

    @Test
    public void insert(){
        Connection conn = null;
        Statement st = null;
        ResultSet rs = null;
        try{
            conn = JdbcUtils.getConnection();
            st = (Statement) conn.createStatement();
            String sql = "insert into users(id, name, password, email, birthday) values(1, 'xxx', '123', 'xx@sina.com', '1980-09-09')";
            int num = st.executeUpdate(sql);
            if (num > 0) {
                System.out.println("插入成功!!");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.release(conn, st, rs);
        }
    }

    @Test
    public void delete(){
        Connection conn = null;
        Statement st = null;
        ResultSet rs = null;
        try{
            conn = JdbcUtils.getConnection();
            String sql = "delete from users where id=4";
            st = (Statement) conn.createStatement();
            int num = st.executeUpdate(sql);
            if (num>0) {
                System.out.println("删除成功！！");
            }
        }catch (Exception e) {

        }finally {
            JdbcUtils.release(conn, st, rs);
        }
    }

    @Test
    public void update(){
        Connection conn = null;
        Statement st = null;
        ResultSet rs = null;
        try{
            conn = JdbcUtils.getConnection();
            String sql = "update users set name='wuwang',email='wuwang@sina.com' where id=4";
            st = (Statement) conn.createStatement();
            int num = st.executeUpdate(sql);
            if (num > 0){
                System.out.println("更新成功！！");
            }
        }catch (Exception e) {

        }finally {
            JdbcUtils.release(conn, st, rs);
        }
    }

    @Test
    public void find(){
        Connection conn = null;
        Statement st = null;
        ResultSet rs = null;
        try{
            conn = JdbcUtils.getConnection();
            String sql = "select * from users where id=4";
            st = (Statement) conn.createStatement();
            rs = st.executeQuery(sql);
            if (rs.next()) {
                System.out.println(rs.getString("name"));
            }
        }catch (Exception e) {

        } finally {
            JdbcUtils.release(conn, st, rs);
        }
    }
}
